<template>
  <div class="p-2 container">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="分组名称" prop="cameraGroupName">
              <el-input v-model="queryParams.cameraGroupName" placeholder="请输入分组名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
<!--            <el-form-item label="创建时间" prop="createTime">-->
<!--              <el-date-picker v-model="queryParams.createTime" type="date" value-format="YYYY-MM-DD" clearable placeholder="请选择" />-->
<!--            </el-form-item>-->
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never" style="flex: 1; display: flex; flex-direction: column; position: relative;">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['camera:equipment-camera-group:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['camera:equipment-camera-group:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()">删除</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="cameraGroupList" height="calc(100% - 30px)" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="分组名称" align="center" prop="cameraGroupName" show-overflow-tooltip />
        <el-table-column label="备注说明" align="center" prop="remark" show-overflow-tooltip />
        <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip />
        <el-table-column label="操作" fixed="right" :min-width="100" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button v-if="scope.row.equipmentCameraGroupId != 1" v-hasPermi="['camera:equipment-camera-group:edit']" link type="primary" @click="openUpdate(scope.row)">修改</el-button>
            <el-button v-if="scope.row.equipmentCameraGroupId != 1" v-hasPermi="['camera:equipment-camera-group:remove']" link type="primary" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <!-- 添加摄像头分组对话框 -->
    <el-drawer v-model="dialog.shouAdd" :title="dialog.title" width="500px" append-to-body destroy-on-close>
     <add @close="dialog.visible = false" @success="getList()"></add>
    </el-drawer>

    <!-- 修改摄像头分组对话框 -->
    <el-drawer v-model="dialog.showUpdate" :title="dialog.title" width="500px" append-to-body destroy-on-close>
     <update :equipment-camera-group-id="equipmentCameraGroupId" @close="dialog.showUpdate = false" @success="getList()"></update>
    </el-drawer>
  </div>
</template>

<script setup name="cameraGroup" lang="ts">
import { cameraGroupVO, cameraGroupQuery, cameraGroupForm } from '@/api/security/camera/camera-group/types';
import request from '@/utils/request';
import add from './components/add.vue';
import update from './components/update.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const cameraGroupList = ref<cameraGroupVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: cameraGroupForm = {
  remark: undefined,
  cameraGroupName: undefined,
  parkId: undefined
}
const data = reactive<PageData<cameraGroupForm, cameraGroupQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: { }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询摄像头分组列表 */
const getList = async () => {
  loading.value = true;
  const res = await request({ url: '/equipment/equipment-camera-group/list', method: 'get', params: queryParams.value }).finally(() => loading.value = false);
  cameraGroupList.value = res.rows;
  total.value = res.total;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: cameraGroupVO[]) => {
  ids.value = selection.map(item => item.equipmentCameraGroupId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  dialog.shouAdd = true;
  dialog.title = '添加摄像头分组';
}

const equipmentCameraGroupId = ref();
/** 打开修改弹框 */
const openUpdate = async (row?: cameraGroupVO) => {
  equipmentCameraGroupId.value = row?.equipmentCameraGroupId || ids.value[0]

  dialog.showUpdate = true;
  dialog.title = '修改摄像头分组';
}

/** 删除按钮操作 */
const handleDelete = async (row?: cameraGroupVO) => {
  const _cameraGroupIds = row?.equipmentCameraGroupId || ids.value;
  await proxy?.$modal.confirm('是否确认删除摄像头分组编号为"' + _cameraGroupIds + '"的数据项？').finally(() => loading.value = false);
  await request({ url: '/equipment/equipment-camera-group/' + _cameraGroupIds, method: 'delete' })
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
}

onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
    :deep(.el-card__header) {
        height: 60px;
    }
    :deep(.el-card__body) {
        height: calc(100% - 60px);
    }
    :deep(.pagination-container) {
        margin-top: 0;
    }
</style>
